package com.avic.rong.search.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.bean.product.GoodBean;
import com.avic.rong.goodsSort.api.StandardSearchApi;
import com.avic.rong.search.adapter.homePageAdapter.TransformAdapter;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchTransformFragment extends Fragment implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.recycler_list)
    RecyclerView recycler_list;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    String currentText = "GJB";
    private static int pageNo = 1;
    private Context mContext;
    Unbinder unbinder;

    private TransformAdapter transformAdapter;
    private StandardSearchApi standardSearchApi;

    public SearchTransformFragment() {
        // Required empty public constructor
    }


    public static SearchTransformFragment getInstance(Context context) {
        SearchTransformFragment transformFragment = new SearchTransformFragment();
        transformFragment.mContext = context;
        return transformFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_transform, container, false);
        unbinder = ButterKnife.bind(this, view);


        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);

        recycler_list.setLayoutManager(new LinearLayoutManager(getActivity()));

        transformAdapter = new TransformAdapter(mContext,0);
//        adapter = new SearchVpBzRvAdapter(getContext(), list);
//        adapter.setListener(SearchTransformFragment.this);
//        searchBiaoZhunRv.setAdapter(adapter);
        recycler_list.setAdapter(transformAdapter);


        standardSearchApi = new StandardSearchApi((BaseActivity) mContext, searchAllListener);
        standardSearchApi.setTypes(0);
        standardSearchApi.setKeyword("gjb");
        HttpManager.getInstance().doHttpDeal(standardSearchApi);




        return view;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//        list.clear();
//        pageNo = 1;
//        initBzData(currentText, pageNo);
//        searchFragmentBzSmartRefreshLayout.finishRefresh(1000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//        ++pageNo;
//        initBzData(currentText, pageNo);
//        searchFragmentBzSmartRefreshLayout.finishLoadMore(1000);
    }



    //获取标准数据监听
    HttpOnNextListener<BaseResultEntity> searchAllListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

//            loading_layout.setStatus(LoadingLayout.SUCCESS);

//            CircleProgressDialog.closeDialog();
            if (!ConstantsUtils.STATUS_SUCCESS_200.equals(subjects.getCode())) {
                ToastTools.showToast(mContext, subjects.getMsg());
                Logger.e("cjx", "biaozhun数据：" + subjects.getMsg());
                return;
            }

            List<GoodBean> tempList = new ArrayList<>();
            GoodBean goodBean = new GoodBean();
            goodBean.setTitle("asdfasdf");

            tempList.add(goodBean);

//            transformAdapter.setBeanList(tempList);
//            transformAdapter.notifyDataSetChanged();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
        }
    };





    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }


}
